/*
 * tab_crash_curve.cpp
 *
 *  Created on: 16.06.2013
 *      Author: SpirTanol
 */

#include "ui/tab_crash_curve.h"
#include "ui/crash_plot.h"
#include "events/data_event.h"
#include "device/device_info.h"
#include "ui/main_window.h"
#include "packets/crash_oscillogram_packet.h"

// QT
#include <QLabel>
#include <QGridLayout>
#include <QDateTime>
//======================================================================================
TabCrashCurve::TabCrashCurve(MainWindow *mainWindow) : oMainWindow(mainWindow), oPlotWriteTime(0), oCrashCode(0)
{
	QGridLayout *l = new QGridLayout(this);

	oLabelCrashText = new QLabel(trUtf8("crash text"));
	l->addWidget(oLabelCrashText, 0, 0);

	oLabelCrashTime = new QLabel(trUtf8("crash time"));
	l->addWidget(oLabelCrashTime, 1, 0);

	oPlot = new CrashPlot();
	l->addWidget(oPlot, 2, 0);
}

TabCrashCurve::~TabCrashCurve()
{

}
//======================================================================================
void TabCrashCurve::onDataEvent(const DataEvent *e)
{
	switch (e->type())
	{
	case DET_CONNECT:
		oPlot->setMinCurrent(oMainWindow->deviceInfo()->curMin());
		break;
	case DET_CRASH_OSCILLOGRAM_GET:
	{
		const CrashOscillogramPacket* oscillogram = static_cast<const CrashEventOscillogramGet*>(e)->oscillogram();
		oPlot->updateData(oscillogram);

		oCrashCode = oscillogram->code();
		oLabelCrashText->setText(crashText(oCrashCode));
		oPlotWriteTime = oscillogram->time();
		oLabelCrashTime->setText(QDateTime::fromTime_t(oscillogram->time()).toString("dd.MM.yyyy hh:mm:ss"));
	}
		break;
	default: break;
	}
}
